const domesticPlaces = [
    { name: "广西", members: "26657人已加入", image: "https://p1-q.mafengwo.net/s17/M00/37/C6/CoUBXl_iwriAZRszAACbUxJZnoo09.jpeg?imageView2/1/w/480/h/260" },
    { name: "四川", members: "41125人已加入", image: "https://p1-q.mafengwo.net/s17/M00/A5/CF/CoUBXl_bQP6AC_3KAAEC1FJizT490.jpeg?imageView2/1/w/480/h/260" },
    { name: "内蒙古", members: "22558人已加入", image: "https://p1-q.mafengwo.net/s17/M00/A5/E4/CoUBXl_bQSKAUeU3AAGAxgrRgeU73.jpeg?imageView2/1/w/480/h/260" },
    { name: "西藏", members: "21444人已加入", image: "https://p1-q.mafengwo.net/s17/M00/4C/51/CoUBXl_gP8-AUlxCAAEhsF_KAx846.jpeg?imageView2/1/w/480/h/260" },
    { name: "云南", members: "55615人已加入", image: "https://p1-q.mafengwo.net/s17/M00/A5/BD/CoUBXl_bQOCAJvEmAAI_-Pgysd855.jpeg?imageView2/1/w/480/h/260" },
    { name: "贵州", members: "30994人已加入", image: "https://p1-q.mafengwo.net/s16/M00/AC/9F/CoUBUl_lmyCAdujaAACl-pGLdlw69.jpeg?imageView2/1/w/480/h/260" },
    { name: "北京", members: "38542人已加入", image: "https://p1-q.mafengwo.net/s16/M00/05/86/CoUBUl_ZydWAFJLPAAVNFTCqozw74.jpeg?imageView2/1/w/480/h/260" },
    { name: "福建", members: "41258人已加入", image: "https://p1-q.mafengwo.net/s17/M00/E4/A7/CoUBXl_cbGeACFoMAAKDgr_bhUo06.jpeg?imageView2/1/w/480/h/260" },
    { name: "广东", members: "32687人已加入", image: "https://p1-q.mafengwo.net/s16/M00/99/3C/CoUBUl_cauGAGtLIAAGfhx2mJGI60.jpeg?imageView2/1/w/480/h/260" },
    { name: "广西", members: "26657人已加入", image: "https://p1-q.mafengwo.net/s17/M00/37/C6/CoUBXl_iwriAZRszAACbUxJZnoo09.jpeg?imageView2/1/w/480/h/260" },
    { name: "四川", members: "41125人已加入", image: "https://p1-q.mafengwo.net/s17/M00/A5/CF/CoUBXl_bQP6AC_3KAAEC1FJizT490.jpeg?imageView2/1/w/480/h/260" },
    { name: "内蒙古", members: "22558人已加入", image: "https://p1-q.mafengwo.net/s17/M00/A5/E4/CoUBXl_bQSKAUeU3AAGAxgrRgeU73.jpeg?imageView2/1/w/480/h/260" },
    { name: "西藏", members: "21444人已加入", image: "https://p1-q.mafengwo.net/s17/M00/4C/51/CoUBXl_gP8-AUlxCAAEhsF_KAx846.jpeg?imageView2/1/w/480/h/260" },
    { name: "云南", members: "55615人已加入", image: "https://p1-q.mafengwo.net/s17/M00/A5/BD/CoUBXl_bQOCAJvEmAAI_-Pgysd855.jpeg?imageView2/1/w/480/h/260" },
    { name: "贵州", members: "30994人已加入", image: "https://p1-q.mafengwo.net/s16/M00/AC/9F/CoUBUl_lmyCAdujaAACl-pGLdlw69.jpeg?imageView2/1/w/480/h/260" },
    { name: "北京", members: "38542人已加入", image: "https://p1-q.mafengwo.net/s16/M00/05/86/CoUBUl_ZydWAFJLPAAVNFTCqozw74.jpeg?imageView2/1/w/480/h/260" },
    { name: "福建", members: "41258人已加入", image: "https://p1-q.mafengwo.net/s17/M00/E4/A7/CoUBXl_cbGeACFoMAAKDgr_bhUo06.jpeg?imageView2/1/w/480/h/260" },
    { name: "广东", members: "32687人已加入", image: "https://p1-q.mafengwo.net/s16/M00/99/3C/CoUBUl_cauGAGtLIAAGfhx2mJGI60.jpeg?imageView2/1/w/480/h/260" }
];

const internationalPlaces = [
    { name: "韩国 (KR)", members: "9543人已加入", image: "https://p1-q.mafengwo.net/s17/M00/E8/4E/CoUBXl_tnkKAYm-YAAGq4NLAL0M91.jpeg?imageView2/1/w/480/h/260" },
    { name: "新加坡 (SG)", members: "10462人已加入", image: "https://p1-q.mafengwo.net/s16/M00/06/9E/CoUBUl_tpHGAcjIeAAIw1jfrsmI99.jpeg?imageView2/1/w/480/h/260" },
    { name: "越南 (VN)", members: "7177人已加入", image: "https://p1-q.mafengwo.net/s16/M00/06/9E/CoUBUl_tpHGAcjIeAAIw1jfrsmI99.jpeg?imageView2/1/w/480/h/260" },
    { name: "马来西亚 (MY)", members: "18262人已加入", image: "https://p1-q.mafengwo.net/s16/M00/F5/A8/CoUBUl_rBpyAeS5EAAET4F4w_CA47.jpeg?imageView2/1/w/480/h/260" },
    { name: "日本 (JP)", members: "42730人已加入", image: "https://p1-q.mafengwo.net/s19/M00/07/C6/CoNB_GXAhSEsaR8tAAHI_FwSWHs.jpeg?imageView2/1/w/480/h/260" },
    { name: "泰国 (TH)", members: "29329人已加入", image: "https://p1-q.mafengwo.net/s17/M00/D0/E6/CoUBXl_rB3-AOURzAAHfcJmmq-489.jpeg?imageView2/1/w/480/h/260" },
    { name: "埃及 (EG)", members: "15678人已加入", image: "https://p1-q.mafengwo.net/s10/M00/A5/22/wKgBZ1mSnR2AbCH7ABAZrFNuVGw00.jpeg?imageView2/1/w/480/h/260" },
    { name: "阿联酋 (AE)", members: "12453人已加入", image: "https://p1-q.mafengwo.net/s19/M00/77/46/CoNKtmUEFS4eG8BsAAMZ_5Lm_pc.jpeg?imageView2/1/w/480/h/260" },
    { name: "捷克 (CZ)", members: "11876人已加入", image: "https://p1-q.mafengwo.net/s7/M00/9E/4E/wKgB6lTJ7ZiANWlWAARS81EK1-072.jpeg?imageView2/1/w/480/h/260" },
    { name: "韩国 (KR)", members: "9543人已加入", image: "https://p1-q.mafengwo.net/s17/M00/E8/4E/CoUBXl_tnkKAYm-YAAGq4NLAL0M91.jpeg?imageView2/1/w/480/h/260" },
    { name: "新加坡 (SG)", members: "10462人已加入", image: "https://p1-q.mafengwo.net/s16/M00/06/9E/CoUBUl_tpHGAcjIeAAIw1jfrsmI99.jpeg?imageView2/1/w/480/h/260" },
    { name: "越南 (VN)", members: "7177人已加入", image: "https://p1-q.mafengwo.net/s16/M00/06/9E/CoUBUl_tpHGAcjIeAAIw1jfrsmI99.jpeg?imageView2/1/w/480/h/260" },
    { name: "马来西亚 (MY)", members: "18262人已加入", image: "https://p1-q.mafengwo.net/s16/M00/F5/A8/CoUBUl_rBpyAeS5EAAET4F4w_CA47.jpeg?imageView2/1/w/480/h/260" },
    { name: "日本 (JP)", members: "42730人已加入", image: "https://p1-q.mafengwo.net/s19/M00/07/C6/CoNB_GXAhSEsaR8tAAHI_FwSWHs.jpeg?imageView2/1/w/480/h/260" },
    { name: "泰国 (TH)", members: "29329人已加入", image: "https://p1-q.mafengwo.net/s17/M00/D0/E6/CoUBXl_rB3-AOURzAAHfcJmmq-489.jpeg?imageView2/1/w/480/h/260" },
    { name: "埃及 (EG)", members: "15678人已加入", image: "https://p1-q.mafengwo.net/s10/M00/A5/22/wKgBZ1mSnR2AbCH7ABAZrFNuVGw00.jpeg?imageView2/1/w/480/h/260" },
    { name: "阿联酋 (AE)", members: "12453人已加入", image: "https://p1-q.mafengwo.net/s19/M00/77/46/CoNKtmUEFS4eG8BsAAMZ_5Lm_pc.jpeg?imageView2/1/w/480/h/260" },
    { name: "捷克 (CZ)", members: "11876人已加入", image: "https://p1-q.mafengwo.net/s7/M00/9E/4E/wKgB6lTJ7ZiANWlWAARS81EK1-072.jpeg?imageView2/1/w/480/h/260" }
];

// 分页变量
let domesticPage = 1;
let internationalPage = 1;
let isLoading = false;

// 动态获取每页项目数
function getItemsPerPage() {
    const width = window.innerWidth;
    if (width >= 992) return 9; // 大屏幕3x3
    if (width >= 768) return 6; // 中等2x3
    return 4; // 小屏幕1x4
}

// 初始化页面
document.addEventListener('DOMContentLoaded', function() {
    loadInitialDomestic();
    loadInitialInternational();
    setupScrollListener();
    
    // 确保初始加载足够内容
    setTimeout(() => {
        window.dispatchEvent(new Event('scroll'));
    }, 500);
});

// 加载初始国内数据
function loadInitialDomestic() {
    const container = document.getElementById('domestic-content');
    const itemsPerPage = getItemsPerPage();
    const initialData = domesticPlaces.slice(0, itemsPerPage);
    
    initialData.forEach(place => {
        container.appendChild(createCard(place));
    });
    
    domesticPage++;
}

// 加载初始国际数据
function loadInitialInternational() {
    const container = document.getElementById('international-content');
    const itemsPerPage = getItemsPerPage();
    const initialData = internationalPlaces.slice(0, itemsPerPage);
    
    initialData.forEach(place => {
        container.appendChild(createCard(place));
    });
    
    internationalPage++;
}

// 创建卡片元素
function createCard(place) {
    const col = document.createElement('div');
    col.className = 'col-md-4 col-sm-6';
    
    col.innerHTML = `
        <div class="card destination-card h-100 shadow-sm">
            <div class="card-img-top destination-image" style="height: 160px; background-image: url('${place.image}'); background-size: cover; background-position: center; position: relative;">
                <span class="position-absolute bottom-0 end-0 badge tag m-2">攻略群</span>
            </div>
            <div class="card-body text-center">
                <h5 class="card-title">${place.name}</h5>
                <p class="text-muted small">${place.members}</p>
                <button class="btn btn-dark join-btn w-100">立即入群 +</button>
            </div>
        </div>
    `;
    
    // 添加点击事件
    col.querySelector('.join-btn').addEventListener('click', function() {
        alert(`您已申请加入${place.name}攻略群，稍后会有客服联系您！`);
    });
    
    return col;
}

// 加载更多国内数据
function loadMoreDomestic() {
    if (isLoading) return;
    
    const loader = document.getElementById('domestic-loader');
    const container = document.getElementById('domestic-content');
    const itemsPerPage = getItemsPerPage();
    const startIndex = (domesticPage - 1) * itemsPerPage;
    const endIndex = startIndex + itemsPerPage;
    const moreData = domesticPlaces.slice(startIndex, endIndex);
    
    if (moreData.length === 0) {
        loader.innerHTML = '<p class="text-muted">没有更多内容了</p>';
        return;
    }
    
    isLoading = true;
    loader.classList.remove('d-none');
    
    // 模拟网络请求延迟
    setTimeout(() => {
        moreData.forEach(place => {
            container.appendChild(createCard(place));
        });
        
        domesticPage++;
        isLoading = false;
        loader.classList.add('d-none');
        
        // 检查是否需要加载更多以填满屏幕
        checkViewportFill();
    }, 1000);
}

// 加载更多国际数据
function loadMoreInternational() {
    if (isLoading) return;
    
    const loader = document.getElementById('international-loader');
    const container = document.getElementById('international-content');
    const itemsPerPage = getItemsPerPage();
    const startIndex = (internationalPage - 1) * itemsPerPage;
    const endIndex = startIndex + itemsPerPage;
    const moreData = internationalPlaces.slice(startIndex, endIndex);
    
    if (moreData.length === 0) {
        loader.innerHTML = '<p class="text-muted">没有更多内容了</p>';
        return;
    }
    
    isLoading = true;
    loader.classList.remove('d-none');
    
    // 模拟
    setTimeout(() => {
        moreData.forEach(place => {
            container.appendChild(createCard(place));
        });
        
        internationalPage++;
        isLoading = false;
        loader.classList.add('d-none');
        
        // 检查是否需要加载更多以填满屏幕
        checkViewportFill();
    }, 1000);
}

// 检查视窗是否填满
function checkViewportFill() {
    const activeTab = document.querySelector('.tab-pane.active');
    const tabContent = activeTab.querySelector('.row');
    
    if (tabContent.clientHeight < window.innerHeight - 200) {
        if (activeTab.id === 'domestic') {
            loadMoreDomestic();
        } else if (activeTab.id === 'international') {
            loadMoreInternational();
        }
    }
}

// 设置滚动监听
function setupScrollListener() {
    window.addEventListener('scroll', function() {
        const activeTab = document.querySelector('.tab-pane.active');
        const tabContent = activeTab.querySelector('.row');
        
        // 检查内容是否填满可视区域
        if (tabContent.clientHeight < window.innerHeight) {
            if (activeTab.id === 'domestic' && domesticPage === 1) {
                loadMoreDomestic();
            } else if (activeTab.id === 'international' && internationalPage === 1) {
                loadMoreInternational();
            }
        }
        
        // 原有滚动检查
        const { scrollTop, scrollHeight, clientHeight } = document.documentElement;
        if (scrollTop + clientHeight >= scrollHeight - 100) {
            if (activeTab.id === 'domestic') {
                loadMoreDomestic();
            } else if (activeTab.id === 'international') {
                loadMoreInternational();
            }
        }
    });
    
    // 窗口大小改变时重新计算
    window.addEventListener('resize', function() {
        checkViewportFill();
    });
}